<?php

namespace api\modules\v1\controllers\buss;

use common\helpers\CommonHelper;
use services\linkedcare\LinkedcareChannelService;
use services\user\UserFeedbackService;
use Yii;
use api\controllers\OnAuthController;
use yii\db\Exception;
use yii\web\HttpException;

class UserFeedbackController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/user-feedback/index 客户意见列表
     * @apiGroup UserFeedback
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {string} keywords
     * @apiParam {int} is_reply
     * @apiParam {string} created_stime
     * @apiParam {string} created_etime
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Object}  data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               列表ID【编号】
     * @apiSuccess {String}  data.list.patient_name     客户
     * @apiSuccess {String}  data.list.patient_mobile   手机号
     * @apiSuccess {String}  data.list.is_reply_name    回复状态【1已回复、0未回复（包含清空内容）】
     * @apiSuccess {String}  data.list.content          意见内容
     * @apiSuccess {String}  data.list.reply_content    回复内容
     * @apiSuccess {String}  data.list.created_at_str   创建时间
     * @apiSuccess {String}  data.list.reply_time_str   回复时间
     *
     */
    public function actionIndex()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $service = new UserFeedbackService();
        $list['list'] = $service->userFeedbackList($params, $page, $pageSize);
        $list['condition'] = $service->getReplyCondition();
        return $list;
    }

    /**
     * @throws Exception
     * @api {post} /v1/buss/user-feedback/reply 客户意见回复
     * @apiGroup UserFeedback
     * @apiName reply
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                  客户意见ID
     * @apiBody {String}   reply_content       回复内容
     *
     * @apiSuccess {Number} data    客户意见ID
     */
    public function actionReply()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '请选择要回复的客户意见');
        }
        if (!isset($params['reply_content']) || !$params['reply_content']) {
            $params['reply_content'] = '';
        }
        $service = new UserFeedbackService();
        return $service->reply($params);
    }


    /**
     * @api {get} /v1/buss/user-feedback/export 客户意见导出
     * @apiGroup UserFeedback
     * @apiName export
     * @apiUse CommonParam
     *
     * @apiParam {string} keywords
     * @apiParam {int} is_reply
     * @apiParam {string} created_stime
     * @apiParam {string} created_etime
     *
     */
    public function actionExport()
    {
        $params = Yii::$app->request->get();
        $service = new UserFeedbackService();
        $list = $service->userFeedbackList($params, 0, 0, 1);
        return $service->export($list);
    }
}
